﻿<!DOCTYPE html>

<html lang="zh-cn">
<head>
    <!--<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />-->
    <meta name="viewport" content="width=device-width" />
    <title>编辑模块配置步骤</title>
    <link href="../lib/layui/css/layui.min.css" rel="stylesheet" />
    <link href="../x-admin/css/font.css" rel="stylesheet" />
    <link href="../x-admin/css/xadmin.css" rel="stylesheet" />
    <script src="../lib/cookie/cookie.js"></script>
    <style>
        .ele {
            height: auto;
            width: 100%;
            display: none;
            position: absolute;
            top: 100%;
            background-color: #fff;
            z-index: 100;
        }

        .eleTree {
            height: 210px;
            border: 1px solid #eee;
            overflow: scroll;
            display: inline-block;
        }
        #typediv .layui-form-select dl {
            max-height: 260px;
        }
    </style>
</head>
<body>
    <div class="layui-card-body" style="">
        <form class="layui-form" action="" name="newItem" id="updateItem">
            <input type="text" name="moduleCfgName" id="moduleCfgName" lay-verify="title" autocomplete="off" value="" class="layui-input" style="display:none">
            <input type="text" name="oldNumber" id="oldNumber" lay-verify="title" autocomplete="off" value="" class="layui-input" style="display:none">

            <div class="layui-form-item">
                <label class="layui-form-label">步骤序号:</label>
                <div class="layui-input-block">
                    <input type="text" name="number" id="number" lay-verify="title" autocomplete="off" required value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">执行类型:</label>
                <div class="layui-input-block">
                    <select name="editexecuteType" id="editexecuteType" lay-filter="editexecuteType" lay-verify="required">
                        <option value="xml">xml文件</option>
                        <option value="xml_var">xml自定义</option>
                        <option value="dll">dll包</option>
                        <option value="reg">正则</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">步骤编码:</label>
                <div class="layui-input-block" id="typediv">
                    <input type="text" name="code" lay-verify="title" autocomplete="off" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label" id="executetypename"></label>
                <div class="layui-input-block">
                    <input type="text" name="executeFile" id="executeFile" lay-verify="title" autocomplete="off" value="" class="layui-input">
                    <div class="eleTree ele" id="selectFile" lay-filter="selectFile"></div>
                </div>
            </div>

            <div class="layui-form-item" style="display:none">
                <label class="layui-form-label">执行代码:</label>
                <div class="layui-input-block">
                    <input type="text" name="executeCode" lay-verify="title" autocomplete="off" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">中文名称:</label>
                <div class="layui-input-block">
                    <input type="text" name="zh_cn" lay-verify="title" autocomplete="off" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formmoudleitem">立即提交</button>
                </div>
            </div>
            <!-- <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="" class="layui-btn" lay-submit="" onclick="save(this)">立即提交</button>
        </div>
    </div>-->
        </form>
    </div>
    <script src="../lib/layui/layui.js"></script>
    <script type="text/javascript">
        function getrequest() {
            var url = location.search; //获取url中"?"符后的字串
            var theRequest = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        }
        var pars = getrequest();
        var item = JSON.parse(window.decodeURIComponent(atob(pars.moudleitem)));
        var $ = layui.$;
        
        $().ready(function () {
            $("#selectFile").hide();
            $("#oldNumber").val(item.number);
            $("#number").val(item.number);
            $("#moduleCfgName").val(pars.fileName);
            //$("input[name='code']").val(item.code);
            $("input[name='zh_cn']").val(item.moduleName);
            $("#editexecuteType option[value='" + item.executeType + "']").attr("selected", true);
            switch (item.executeType) {
                case 'reg':
                    $("#executetypename").text('正则编号:');
                    $("input[name='executeFile']").removeAttr("readonly");
                    $("input[name='executeFile']").val(item.executeCode);
                    getcodeinfos("input");
                    $("input[name='code']").val(item.code);
                    //$("#editexecuteType").find("option:contains('正则')").attr("selected", true);
                    break;
                case 'xml':
                    $("#executetypename").text('配置文件:');
                    $("input[name='executeFile']").attr("readonly", "readonly");
                    $("input[name='executeFile']").val(item.filePath);
                    getcodeinfos();
                    //$("#editexecuteType option[value='" + xml + "']").attr("selected", true);
                    $("#code option[value='" + item.code+"']").attr("selected", true);
                    //$("#editexecuteType").find("option:contains('xml文件')").attr("selected", true);
                    break;
                case 'dll':
                    $("#executetypename").text('配置文件:');
                    $("input[name='executeFile']").attr("readonly", "readonly");
                    $("input[name='executeFile']").val(item.filePath);
                    getcodeinfos();
                    $("#code option[value='" + item.code + "']").attr("selected", true);
                    //$("#editexecuteType").find("option:contains('dll包')").attr("selected", true);
                    break;
                case 'xml_var':
                    $("#executetypename").text('配置文件:');
                    $("input[name='executeFile']").val(item.filePath);
                    getcodeinfos();
                    $("#code option[value='" + item.code + "']").attr("selected", true);
                    //$("#editexecuteType").find("option:contains('xml自定义')").attr("selected", true);
                default:
            }
            //需要进行重新渲染
            layui.form.render('select');
        });

        function getcodeinfos(type) {
            if (type == "input") {
                //步骤编号是输入框
                $("#typediv").html("<input type=\"text\" name=\"code\" id=\"code\" lay-verify=\"title\" autocomplete=\"off\" required value=\"\" class=\"layui-input\">");
            } else {
                //步骤编号是下拉框
                //获取配置文件信息
                $.ajax({
                    url: "../xmlstepcode.json",
                    async: false,
                    type: "GET",
                    dataType: "json",
                    success: function (jsonm) {
                        // 写入数据
                        var output = [];
                        $.each(jsonm, function (key, value) {
                            output.push('<option value="' + key + '">' + value + '</option>');
                        });
                        $("#typediv").html("<select name='code' id='code'  lay-verify='required' lay-search=''>" + output.join('') + "</select >");
                        
                    }
                });
            }
        }
        layui.config({
            base: '/lib/layui/lay/plugins/'
        }).extend({
            treeSelect: 'treeSelect/treeSelect',
            eleTree: 'eleTree/eleTree'
        }).use(['form', 'treeSelect', 'eleTree'], function () {

            var form = layui.form;
            var eleTree = layui.eleTree;
            //执行类型下拉框change事件
            layui.form.on('select(editexecuteType)', function (obj) {
                var executeType = obj.value;
                // 当filetype时xml或是dll时下拉出对应类型的文件，否则设置成可输的编辑框(此时主要对应的正则时候输入正则编号)
                switch (executeType) {
                    case 'reg':
                        $("#executeFile").removeAttr("readonly");
                        $("#executetypename").text("正则编号:");
                        $("#executeFile").attr("placeholder", "请输入正则编号");
                        $("#selectFile").hide();
                        getcodeinfos("input");
                        break;
                    case 'xml_var':
                        $("#executeFile").removeAttr("readonly");
                        $("#selectFile").hide();
                        $("#executeFile").attr("placeholder", "文件格式：a/b/c/{casetype}_{causecode}.html");
                        $("#executetypename").text("自定义文件:");
                        getcodeinfos();
                        break;
                    case 'xml':
                    case 'dll':
                        $("#executeFile").removeAttr("placeholder");
                        $("#executeFile").attr("readonly", "readonly");
                        $("#executetypename").text("配置文件:");
                        $("#selectFile").hide();
                        getcodeinfos();
                        break;
                }
                layui.form.render('select');
            });
            //监听提交
            form.on('submit(formmoudleitem)', function (data) {
                //layer.msg(JSON.stringify(data.field));
                $.ajax({
                    type: "post",
                    url: localStorage.getItem('hosttingaddr') + '/api/moudlecfgfile/uploadfileofitem',
                    headers: {
                        user: getCookie("userinfo"),
                        version: getCookie("version")
                    },
                    data: $('#updateItem').serialize(),
                    success: function (data) {
                        if (data == "success") {
                            layer.msg('更新完成!', { icon: 1 }, function () {
                                parent.layui.table.reload('moudlestepinfo');
                                var index = parent.layer.getFrameIndex(window.name); //获取当前窗口的name
                                parent.layer.close(index);
                            });
                        }
                    },
                    error: function (data) {
                        layer.msg(data.responseText, { icon: 5 });
                    }
                });
                return false;
            });

            var el = eleTree({
                el: '#selectFile',
                method: 'get',
                url: localStorage.getItem('hosttingaddr') + '/api/Commonconfig/GetCommonConfigTree',
                headers: {
                    user: getCookie("userinfo"),
                    version: getCookie("version")
                },
                defaultExpandAll: false,
                expandOnClickNode: false,
                highlightCurrent: true,
                showLine: true,
                response: {
                    statusName: 'code',
                    statusCode: 0,
                    dataName: 'data'
                },
                request: {
                    name: 'name',
                    key: 'fullName',
                    children: 'children',
                    checked: 'checked',
                    disabled: 'disabled',
                    isLeaf: 'isLeaf'
                },
                done: function (res) {
                    // res即为你接口返回的信息。
                    console.log(res);
                }
            });
            el.on("click", function (d) {
                if (d.data.noteType == "F") {
                    $("#executeFile").val(d.data.fullName);
                    $("#selectFile").hide();
                }

            }) 
            //执行类型下拉框change事件
            form.on('select(editexecuteType)', function (obj) {
                var executeType = obj.value;
                // 当filetype时xml或是dll时下拉出对应类型的文件，否则设置成可输的编辑框(此时主要对应的正则时候输入正则编号)
                switch (executeType) {
                    case 'reg':
                        $("#executeFile").removeAttr("readonly");
                        $("#selectFile").hide();
                        $("#labname").text("正则编号");
                        $("#executeFile").attr("placeholder", "请输入正则编号");
                        $("#selectFile").hide();
                        break;
                    case 'xml_var':
                        $("#executeFile").removeAttr("readonly");
                        $("#selectFile").hide();
                        $("#executeFile").attr("placeholder", "文件格式：a/b/c/%casetype%_%cause%.html");
                        $("#labname").text("自定义文件");
                        break;
                    case 'xml':
                    case 'dll':
                        $("#executeFile").removeAttr("placeholder");
                        $("#executeFile").attr("readonly", "readonly");
                        $("#labname").text("配置文件");
                        $("#selectFile").hide();
                        break;
                }
            });
            //配置文件文本框click事件
            $("#executeFile").on("click", function (e) {
                e.stopPropagation();
                var executeType = $("#editexecuteType").val();
                //|| executeType=="xml_var"
                if (executeType == "xml" || executeType == "dll") {
                    $("#selectFile").toggle();
                }

            })
        });
    </script>
</body>
</html>